package vn.qnamuni.calendar.services;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.qnamuni.calendar.activity.SyncActivity;
import vn.qnamuni.calendar.alarm.AlarmHelper;
import vn.qnamuni.calendar.constants.PersonContract;
import vn.qnamuni.calendar.providers.MyContentProvider;
import vn.qnamuni.calendar.utils.User;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class ServiceHelper {

	private Context context;
	private static ServiceHelper serviceHelper;

	public static ServiceHelper getInstance(Context context) {
		if (null == serviceHelper)
			serviceHelper = new ServiceHelper(context);
		// to update the context each time a service helper it's called
		serviceHelper.setContext(context);
		return serviceHelper;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public ServiceHelper(Context context) {
		this.context = context;
	}

	public boolean startService(String action) {
		return startService(action, null);
	}

	// /---------------------------main-------------------
	public boolean isInternetAvailable(Context context) {
		return true;
	}

	public boolean startService(String action, Bundle extras) {
		if (isInternetAvailable(context)) {
			Intent intent = null;
			if (action.equals(FacebookService.GET_INFO)) {
				intent = new Intent(context, FacebookService.class);
				intent.putExtra("action", action);
			} else  
			if (action.equals(FacebookService.POST_WALL)) {
				intent = new Intent(context, FacebookService.class);
				intent.putExtra("action", action);
			}
			context.startService(intent);
			return true;
		} else {
			return false;
		}
	}

	public void processResult(JSONObject jObject) {

		// attribute of json object
		final String TAG_AVATAR_URL = "pic_square";
		final String TAG_NAME = "name";
		final String TAG_ID = "uid";
		final String TAG_DATE = "birthday_date";
		final String TAG_ARRAY = "data"; 


		// parsing
		JSONArray contacts;
		try {
			contacts = new JSONArray(jObject.getString(TAG_ARRAY));

			ContentValues[] value = new ContentValues[contacts.length()];
			
			//we need only 4 attrs: uid, name, birth, avatar
			for (int i = 0; i < contacts.length(); ++i) {

				JSONObject json = contacts.getJSONObject(i);
				// assign to user array

				value[i] = new ContentValues();
				value[i].put(PersonContract.Entry.KEY_UID,
						String.valueOf(json.getLong(TAG_ID)));

				value[i].put(PersonContract.Entry.KEY_NAME,
						json.getString(TAG_NAME));

				value[i].put(PersonContract.Entry.KEY_DATE, 
						json.getString(TAG_DATE));
				
				value[i].put(PersonContract.Entry.KEY_AVATAR,
						json.getString(TAG_AVATAR_URL));

			}
			AlarmHelper alarm = AlarmHelper.getInstance(context);
			if (alarm == null) {
				Log.v("Service helper", " alarm null");
			} else {
				Log.v("Service helper", " alarm ok");
			}
			// values contain all attribute in keyset
			context.getContentResolver().bulkInsert(MyContentProvider.CONTENT_URI,
					value);
		} catch (JSONException e) {
			Log.v("HelperJ", e.getMessage());
		}catch (Exception e) {
			Log.v("Helper", e.getMessage());
		}
	}

	//using user class: easy and accuracy
	public void processResultOld(JSONObject jObject) {

		// attribute of json object
		final String TAG_AVATAR_URL = "pic_square";
		final String TAG_NAME = "name";
		final String TAG_ID = "uid";
		final String TAG_DATE = "birthday_date";
		final String TAG_ARRAY = "data";

		User[] users = null;

		// parsing
		JSONArray contacts;
		try {
			Log.v("pro", "0");
			contacts = new JSONArray(jObject.getString(TAG_ARRAY));
			Log.v("pro", "1");
			users = new User[contacts.length()];

			for (int i = 0; i < contacts.length(); ++i) {

				JSONObject json = contacts.getJSONObject(i);
				// assign to user array

				users[i] = new User();
				users[i].setAttribute(PersonContract.Entry.KEY_UID,
						String.valueOf(json.getLong(TAG_ID)));

				users[i].setAttribute(PersonContract.Entry.KEY_NAME,
						json.getString(TAG_NAME));

				users[i].setAttribute(PersonContract.Entry.KEY_DATE,
						json.getString(TAG_DATE));

				users[i].setAttribute(PersonContract.Entry.KEY_AVATAR,
						json.getString(TAG_AVATAR_URL));

			}

			storeDatabase(users);
		} catch (JSONException e) {

		}
	}

	public void callSync(JSONObject object) {
		Intent sync = new Intent(context, SyncActivity.class);
		sync.putExtra("action", "choose");
		sync.putExtra("json", object.toString());
		sync.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //dùng 1 cờ vì gọi startActivity ko trong Activity
		context.startActivity(sync);
	}
	
	public void storeDatabase(User[] users) {
		// put info to database
		/*
		 * for each user: accept or not
		 */

		ContentValues[] value = new ContentValues[users.length];

		for (int i = 0; i < users.length; ++i) {
			value[i] = new ContentValues();
			for (String key : PersonContract.keySet) {
				if (!key.equals(PersonContract.Entry.KEY_ID)) {
					value[i].put(key, users[i].getAttribute(key));
				}
			}

			// setAlarm for a new user

			/*AlarmHelper.getInstance(context).setAlarm(
					users[i].getAttribute(PersonContract.Entry.KEY_NAME),
					users[i].getAttribute(PersonContract.Entry.KEY_DATE));*/

		}
		// values contain all attribute in keyset
		Log.v("Helper Service", "here");
		AlarmHelper alarm = AlarmHelper.getInstance(context);
		if (alarm == null) {
			Log.v("Service helper", " alarm null");
		} else {
			Log.v("Service helper", " alarm ok");
		}
		
		
		context.getContentResolver().bulkInsert(MyContentProvider.CONTENT_URI,
				value);
	}
}
